﻿using Kugar.Core.ExtMethod;
using Kugar.Web.Wechat.Enums;

namespace Kugar.Web.Wechat.Entities
{
    public interface IWechatRequestBase
    {
        WechatRequestMsgType MsgType { set; get; }

        long MsgId { get; set; }

        /// <summary>
        /// 消息发送者的OpenID
        /// </summary>
        string UserOpenID { set; get; }

        /// <summary>
        /// 消息接收者的公众号账号
        /// </summary>
        string WeiXinAccount { set; get; }
        
        string AppID { set; get; }
    }

    public abstract class WechatRequestBase : IWechatRequestBase
    {
        protected WechatRequestBase()
        {
        }

        public string AppID { get; set; }

        public long MsgId { get; set; }

        public virtual WechatRequestMsgType MsgType { get; set; }

        public string UserOpenID { get; set; }

        public string WeiXinAccount { get; set; }
    }

    public class WechatRequestText: WechatRequestBase
    {
        public override WechatRequestMsgType MsgType { get; set; }= WechatRequestMsgType.Text;

        /// <summary>
        /// 文本消息内容
        /// </summary>
        public string Content { get; set; }

        public static implicit operator WechatRequestText(Senparc.Weixin.MP.Entities.RequestMessageText d)
        {
            return new WechatRequestText()
            {
                Content = d.Content,
                UserOpenID = d.FromUserName,
                WeiXinAccount = d.ToUserName,
                MsgType = WechatRequestMsgType.Text,
                MsgId = d.MsgId
            };
        }
    }

    public class WechatRequestImage : WechatRequestBase
    {
        /// <summary>
        /// 图片消息媒体id，可以调用多媒体文件下载接口拉取数据。
        /// </summary>
        public string MediaId { get; set; }

        public override WechatRequestMsgType MsgType { get; set; } = WechatRequestMsgType.Image;

        /// <summary>
        /// 图片链接
        /// </summary>
        public string PicUrl { get; set; }

        public static implicit operator WechatRequestImage(Senparc.Weixin.MP.Entities.RequestMessageImage d)
        {
            return new WechatRequestImage()
            {
                MediaId = d.MediaId,
                PicUrl = d.PicUrl,
                UserOpenID = d.FromUserName,
                WeiXinAccount = d.ToUserName,
                MsgId = d.MsgId
            };
        }
        
    }

    public class WechatRequestLink : WechatRequestBase
    {
        /// <summary>
        /// 消息描述
        /// </summary>
        public string Description { get; set; }

        public override WechatRequestMsgType MsgType { get; set; } = WechatRequestMsgType.Link;

        /// <summary>
        /// 消息标题
        /// </summary>
        public string Title { get; set; }

        /// <summary>
        /// 消息链接
        /// </summary>
        public string Url { get; set; }

        public static implicit operator WechatRequestLink(Senparc.Weixin.MP.Entities.RequestMessageLink d)
        {
            return new WechatRequestLink()
            {
                Description = d.Description,
                Title = d.Title,
                UserOpenID = d.FromUserName,
                WeiXinAccount = d.ToUserName,
                MsgId = d.MsgId
            };
        }

    }

    public class WechatRequestVoice:WechatRequestBase
    {

        /// <summary>
        /// 语音格式：amr
        /// </summary>
        public string Format { get; set; }

        /// <summary>
        /// 语音消息媒体id，可以调用多媒体文件下载接口拉取数据。
        /// </summary>
        public string MediaId { get; set; }

        public override WechatRequestMsgType MsgType { get; set; } = WechatRequestMsgType.Voice;

        /// <summary>
        /// 语音识别结果，UTF8编码 开通语音识别功能，用户每次发送语音给公众号时，微信会在推送的语音消息XML数据包中，增加一个Recongnition字段。 注：由于客户端缓存，开发者开启或者关闭语音识别功能，对新关注者立刻生效，对已关注用户需要24小时生效。开发者可以重新关注此帐号进行测试。
        /// </summary>
        public string Recognition { get; set; }

        public static implicit operator WechatRequestVoice(Senparc.Weixin.MP.Entities.RequestMessageVoice d)
        {
            return new WechatRequestVoice()
            {
                MediaId = d.MediaId,
                Recognition = d.Recognition,
                Format=d.Format,
                UserOpenID = d.FromUserName,
                WeiXinAccount = d.ToUserName,
                MsgId = d.MsgId
            };
        }
    }

    public class WechatRequestEventBase : WechatRequestBase
    {
        public override WechatRequestMsgType MsgType { get; set; }= WechatRequestMsgType.Event;

        ///<summary>
        /// 事件类型
        /// </summary>   
        public virtual WechatRequestEventType Event { get; set; }
    }

    public class WechatRequestEvent_Subscribe : WechatRequestEventBase
    {
        

        public override WechatRequestEventType Event { get; set; }= WechatRequestEventType.subscribe;

        /// <summary>
        /// 事件KEY值，qrscene_为前缀，后面为二维码的参数值（如果不是扫描场景二维码，此参数为空）
        /// </summary>
        public string EventKey { get; set; }

        public int Scene_id { set; get; }
        

        ///<summary>
        /// 二维码的ticket，可用来换取二维码图片（如果不是扫描场景二维码，此参数为空）
        /// </summary> 
        public string Ticket { get; set; }

        public static implicit operator WechatRequestEvent_Subscribe(Senparc.Weixin.MP.Entities.RequestMessageEvent_Subscribe d)
        {
            var scene ="";

            if (!string.IsNullOrWhiteSpace(d.EventKey))
            {
                scene = d.EventKey.Split('_')[1];
            }

            return new WechatRequestEvent_Subscribe()
            {
                EventKey = d.EventKey,
                Ticket = d.Ticket,
                UserOpenID = d.FromUserName,
                WeiXinAccount = d.ToUserName,
                MsgId = d.MsgId,
                Scene_id = scene.ToInt()
            };
        }
        
    }

    public class WechatRequestEvent_UnSubscribe : WechatRequestEventBase
    {
        public override WechatRequestEventType Event { get; set; } = WechatRequestEventType.unsubscribe;

        public static implicit operator WechatRequestEvent_UnSubscribe(Senparc.Weixin.MP.Entities.RequestMessageEvent_Unsubscribe d)
        {
            return new WechatRequestEvent_UnSubscribe()
            {
                UserOpenID = d.FromUserName,
                WeiXinAccount = d.ToUserName,
                MsgId = d.MsgId
            };
        }
        
    }
    
    public class WechatRequestEvent_Click : WechatRequestEventBase
    {
        public override WechatRequestEventType Event { get; set; } = WechatRequestEventType.CLICK;

        /// <summary>
        /// 事件KEY值，与自定义菜单接口中KEY值对应
        /// </summary>
        public string EventKey { get; set; }

        public static implicit operator WechatRequestEvent_Click(Senparc.Weixin.MP.Entities.RequestMessageEvent_Click d)
        {
            return new WechatRequestEvent_Click()
            {
                EventKey = d.EventKey,
                UserOpenID = d.FromUserName,
                WeiXinAccount = d.ToUserName,
                MsgId = d.MsgId
            };
        }
        
    }

    public class WechatRequestEvent_Scan : WechatRequestEventBase
    {
        public override WechatRequestEventType Event { get; set; } = WechatRequestEventType.scan;


        ///<summary>
        /// 事件KEY值，是一个32位无符号整数，即创建二维码时的二维码scene_id
        /// </summary>   
        public int EventKey { get; set; }

        public string Ticket { get; set; }

        public static implicit operator WechatRequestEvent_Scan(Senparc.Weixin.MP.Entities.RequestMessageEvent_Scan d)
        {
            return new WechatRequestEvent_Scan()
            {
                EventKey = d.EventKey.ToInt(),
                Ticket=d.Ticket,
                UserOpenID = d.FromUserName,
                WeiXinAccount = d.ToUserName,
                MsgId = d.MsgId
            };
        }

    }

    public class WechatRequestEvent_View : WechatRequestEventBase
    {
        public override WechatRequestEventType Event { get; set; } = WechatRequestEventType.VIEW;


        ///<summary>
        /// 事件KEY值，设置的跳转URL
        /// 
        /// </summary> 
        public string Url { get; set; }


        public static implicit operator WechatRequestEvent_View(Senparc.Weixin.MP.Entities.RequestMessageEvent_View d)
        {
            return new WechatRequestEvent_View()
            {
                Url = d.EventKey,
                UserOpenID = d.FromUserName,
                WeiXinAccount = d.ToUserName,
                MsgId = d.MsgId
            };
        }
    }

    public class WechatRequestEvent_ScancodePush : WechatRequestEventBase
    {
        public override WechatRequestEventType Event { get; set; } = WechatRequestEventType.scancode_push;

        ///<summary>
        /// 事件KEY值，与自定义菜单接口中KEY值对应
        /// </summary>   
        public string EventKey { get; set; }

        /// <summary>
        /// 扫描结果
        /// </summary>
        public string ScanResult { get; set; }

        /// <summary>
        /// 扫描的类型
        /// </summary>
        public string ScanType { get; set; }

        public static implicit operator WechatRequestEvent_ScancodePush(Senparc.Weixin.MP.Entities.RequestMessageEvent_Scancode_Push d)
        {
            return new WechatRequestEvent_ScancodePush()
            {
                EventKey = d.EventKey,
                ScanResult=d.ScanCodeInfo?.ScanResult,
                ScanType=d.ScanCodeInfo?.ScanType,
                UserOpenID = d.FromUserName,
                WeiXinAccount = d.ToUserName,
                MsgId = d.MsgId
            };
        }


        public static implicit operator WechatRequestEvent_ScancodePush(Senparc.Weixin.MP.Entities.RequestMessageEvent_Scan d)
        {
            return new WechatRequestEvent_ScancodePush()
            {
                EventKey = d.EventKey,
                ScanResult = d.EventKey,
                ScanType ="",
                UserOpenID = d.FromUserName,
                WeiXinAccount = d.ToUserName,
                MsgId = d.MsgId
            };
        }
    }

    public class WechatRequestEvent_Location : WechatRequestEventBase
    {
        public override WechatRequestEventType Event { get; set; } = WechatRequestEventType.LOCATION;


        /// <summary>
        /// 地理位置维度，事件类型为LOCATION的时存在
        /// </summary>
        public double Latitude { get; set; }


        ///<summary>
        /// 地理位置经度，事件类型为LOCATION的时存在
        /// </summary>   
        public double Longitude { get; set; }

        ///<summary>
        /// 地理位置精度，事件类型为LOCATION的时存在
        /// </summary>   
        public double Precision { get; set; }

        public static implicit operator WechatRequestEvent_Location(Senparc.Weixin.MP.Entities.RequestMessageEvent_Location d)
        {
            return new WechatRequestEvent_Location()
            {
                Latitude = d.Latitude,
                Longitude = d.Longitude,
                Precision = d.Precision,
                UserOpenID = d.FromUserName,
                WeiXinAccount = d.ToUserName,
                MsgId = d.MsgId
            };
        }
    }

    public class WechatRequesEvent_LocationSelect : WechatRequestEventBase
    {
        public override WechatRequestEventType Event { get; set; } = WechatRequestEventType.location_select;

        ///<summary>
        /// 事件KEY值，与自定义菜单接口中KEY值对应
        /// </summary> 
        public string EventKey { get; set; }

        ///<summary>
        /// 地理位置的字符串信息
        /// </summary>   
        public string Label { get; set; }

        /// <summary>
        /// X坐标信息
        /// </summary>
        public string Location_X { get; set; }

        ///<summary>
        /// Y坐标信息
        /// </summary>  
        public string Location_Y { get; set; }

        ///<summary>朋友圈POI的名字，可能为空</summary> 
        public string Poiname { get; set; }

        ///<summary>
        /// 精度，可理解为精度或者比例尺、越精细的话 scale越高
        /// </summary>   
        public string Scale { get; set; }

        public static implicit operator WechatRequesEvent_LocationSelect(Senparc.Weixin.MP.Entities.RequestMessageEvent_Location_Select d)
        {
            return new WechatRequesEvent_LocationSelect()
            {
                EventKey = d.EventKey,
                Label = d.SendLocationInfo?.Label,
                Location_X = d.SendLocationInfo?.Location_X,
                Location_Y = d.SendLocationInfo?.Location_Y,
                Poiname = d.SendLocationInfo?.Poiname,
                Scale = d.SendLocationInfo?.Scale,
                UserOpenID = d.FromUserName,
                WeiXinAccount = d.ToUserName,
                MsgId = d.MsgId
            };
        }
    }
}
 